package com.example.medical.controller.admin;

import com.example.medical.entity.dto.GetListDto;
import com.example.medical.entity.pojo.DeviceType;
import com.example.medical.service.DeviceService;
import com.example.medical.service.DeviceTypeService;
import com.example.medical.utils.common.enums.Code;
import com.example.medical.utils.common.result.Result;
import com.example.medical.utils.config.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/device_type")
public class AdminDeviceTypeController {

    @Autowired
    private DeviceTypeService deviceTypeService;

    @PostMapping("/get_device_type_list")
    public Result AdminGetDeviceList(@RequestBody GetListDto dto){
        return deviceTypeService.AdminGetDeviceList(dto);
    }

    @GetMapping("/get_device_type_detail")
    public Result AdminGetDeviceDetail(@RequestParam("id")Long id){
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        return Result.okResult(deviceTypeService.getById(id));
    }

    @PostMapping("/save_device_type")
    public Result AdminSaveDeviceType(@RequestParam("name")String name){
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        DeviceType deviceType=new DeviceType();
        deviceType.setDeviceTypeName(name);
        deviceTypeService.save(deviceType);
        return Result.okResult(Code.SUCCESS);
    }

    @GetMapping("/remove_device_type")
    public Result AdminRemoveDeviceType(@RequestParam("id")Long id){
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        boolean checkRemove = deviceTypeService.removeById(id);
        if (!checkRemove) {
            return Result.errorResult(Code.SERVER_ERROR);
        }
        return Result.okResult(Code.SUCCESS);
    }

    @GetMapping("/remove_device_type_arr")
    public Result AdminRemoveDeviceTypeArr(@RequestParam("ids")String[] ids){
        return deviceTypeService.AdminRemoveDeviceTypeArr(ids);
    }

    @GetMapping("/update_get_device_type")
    public Result AdminUpdateGetDevice(@RequestParam("id")Long id){
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        return Result.okResult(deviceTypeService.getById(id));
    }

    @PostMapping("/update_device_type")
    public Result AdminUpdateDeviceType(@RequestParam("id")Long id,
                                        @RequestParam("name")String name){
        return deviceTypeService.AdminUpdateDeviceType(id,name);
    }

}
